/*
* This software is licensed under the GNU General Public License, version 2
* You may copy, distribute and modify the software as long as you track changes/dates of in source files and keep all modifications under GPL. You can distribute your application using a GPL library commercially, but you must also provide the source code.

* DNNspot Software (http://www.dnnspot.com)
* Copyright (C) 2013 Atriage Software LLC
* Authors: Kevin Southworth, Matthew Hall, Ryan Doom

* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

* Full license viewable here: http://www.gnu.org/licenses/gpl-2.0.txt
*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DNNspot.ChatRoom.DataModel
{


    public partial class UserQuery : esUserQuery
    {
        public UserQuery()
        {

        }
    }

    public partial class User : esUser
    {



        public static void AddUser(int portalId, int moduleId, int userId, string userHandle, string ip)
        {
            if (!string.IsNullOrEmpty(userHandle))
            {
                var user = new User();
                user.PortalId = portalId;
                user.ModuleId = moduleId;
                user.UserId = userId;
                user.UserHandle = userHandle;
                user.Ip = ip;
                user.Save();
            }
        }

        public static void SetAwayStatus(int portalId, int moduleId, string userHandle, bool isAway, string awayMessage)
        {
            var user = GetUser(portalId, moduleId, userHandle);
            user.IsAway = isAway;
            user.AwayMessage = awayMessage;
            user.Save();
        }

        public static void SetModifiedDate(int portalId, int moduleId, string userHandle)
        {
            SetModifiedDate(portalId, moduleId, userHandle, DateTime.Now);
        }

        public static void SetModifiedDate(int portalId, int moduleId, string userHandle, DateTime dateTime)
        {
            var q = new UserQuery();
            q.Where(q.PortalId == portalId);
            q.Where(q.ModuleId == moduleId);

            q.Where(q.UserHandle == userHandle);

            var user = new User();

            if (user.Load(q))
            {
                user.ModifiedDate = dateTime;
                user.Save();
            }
        }

        public static void RemoveUser(int portalId, int moduleId, string userHandle)
        {
            var q = new UserQuery();
            q.Where(q.PortalId == portalId);
            q.Where(q.ModuleId == moduleId);

            q.Where(q.UserHandle == userHandle);

            var user = new User();

            if (user.Load(q))
            {
                user.MarkAsDeleted();
                user.Save();
            }
        }

        public static bool UserExists(int portalId, int moduleId, int userId, string userHandle)
        {
            var q = new UserQuery();
            q.Where(q.PortalId == portalId);
            q.Where(q.ModuleId == moduleId);

            if (userId < 0)
                q.Where(q.UserHandle == userHandle);
            else
                q.Where(q.UserId == userId || q.UserHandle == userHandle);

            var users = new UserCollection();
            users.Load(q);

            return users.Count > 0;
        }

        public static User GetUser(int portalId, int moduleId, string userHandle)
        {
            var q = new UserQuery();
            q.Where(q.PortalId == portalId);
            q.Where(q.ModuleId == moduleId);
            q.Where(q.UserHandle == userHandle);

            var user = new User();

            if (user.Load(q))
                return user;

            return null;
        }

        public static bool GetAwayStatus(int portalId, int moduleId, string userHandle)
        {
            return Convert.ToBoolean(GetUser(portalId, moduleId, userHandle).IsAway);
        }
    }

    public partial class UserCollection : esUserCollection
    {
        public static List<User> GetUsers(int portalId, int moduleId)
        {
            var q = new UserQuery();
            q.Where(q.PortalId == portalId);
            q.Where(q.ModuleId == moduleId);
            q.OrderBy(q.UserHandle.Ascending);

            var users = new UserCollection();
            users.Load(q);

            return users.ToList();
        }

        public static void RemoveOldUsers(int portalId, int moduleId, TimeSpan age)
        {
            var q = new UserQuery();
            q.Where(q.PortalId == portalId);
            q.Where(q.ModuleId == moduleId);
            q.Where(q.ModifiedDate <= (DateTime.Now - age));

            var users = new UserCollection();
            users.Load(q);
            users.MarkAllAsDeleted();
            users.Save();
        }

        public static int GetUserCount()
        {
            UserCollection collection = new UserCollection();
            collection.LoadAll();

            return collection.Count;
        }
    }
}
